﻿//var cityPage = 0;
function initSite() {
    //热点城市ID查询

    //加载首页城市介绍数据--需修改调用/Home/HotCity/1，查到
    initMap();
    initMarkerManager();
    
    getCityMarker();


}

function initMarkerManager() {
    markerManager = new MarkerManager(map);
    markerManager.refresh();
}

function getAddress(overlay, latlng) {
    if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
    }
}

function initMap() {

    if (GBrowserIsCompatible()) {

        var lat = 35.442425;   // zoom to the whole japan
        var lng = 139.646286;
        var scale = 13;

        map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.continuousZoomEnabled();
        map.setCenter(new GLatLng(lat, lng), scale);

        map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 20)));

    } // GBrowserIsCompatible
}


function createShop(latlng, shopMessage, shopID, shopLogo, shopType, index) {

    var marker = new createMarker(latlng, index, shopID);

    markerManager.addMarker(marker, 1);
    markerManager.refresh();

    //点击了以后弹出小窗口
    GEvent.addListener(marker, "click", function() {
        var myHtml = "<table align='left' id=''><tr><td align='left' width='260px'><img src=" + shopLogo + " height='80px' width='100px' hspace='5' align='left'>" + "</img>" + shopMessage + "</td></tr><tr><td><a href = '/City/Trademark/" + marker.value + "'>更多详细信息</a></td></tr></table>";
        map.openInfoWindowHtml(latlng, myHtml);

    });

    GEvent.addListener(marker, "popup", function() {
        var myHtml = "<table align='left' id=''><tr><td align='left' width='260px'><img src=" + shopLogo + " height='80px' width='100px' hspace='5' align='left'>" + "</img>" + shopMessage + "</td></tr><tr><td><a href = '/City/Trademark/" + marker.value + "'>更多详细信息</a></td></tr></table>";
        map.openInfoWindowHtml(latlng, myHtml);


    });


    shopList.push(marker);
    return marker;
}

function getCityMarker() {
    
    var bound = map.getBounds();

    var northEast = bound.getNorthEast();
    var southWest = bound.getSouthWest();

    var categoryID = document.getElementById("categoryID").value;
    var cityID = document.getElementById("cityID").value;
    var pageID = document.getElementById("pageID").value;
   // alert(categoryID + "," + cityID + "," + pageID);
    var param = "/City/GetCityMarker/" + northEast.lat() + "," + northEast.lng() + "," + southWest.lat() + "," + southWest.lng() + "," + cityID + "," + categoryID + "," + pageID;
    $.getJSON(param, { t: new Date() }, function(j) {

        var arr = new Array();

        for (var i = 0; i < j.length; i++) {
            var value = j[i];
            //alert(value);
            arr = value.split('|');

            var lat = arr[0];
            var lng = arr[1];

            var shopID = arr[2];
            var shopLogo = arr[3];
            var shopMessage = arr[4];
            var shopType = arr[5];
            var marker = createShop(new GLatLng(lat, lng), shopMessage, shopID, shopLogo, shopType, i);


        }

    });
}



function createMarker(point, index, shopID) {

    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon();
    letteredIcon.image = "../../Content/img/shopType/marker" + letter + ".png";
    letteredIcon.shadow = "../../Content/img/shopType/shadow50.png";
    letteredIcon.iconSize = new GSize(20, 34);
    letteredIcon.shadowSize = new GSize(37, 34);
    letteredIcon.iconAnchor = new GPoint(9, 34);
    letteredIcon.infoWindowAnchor = new GPoint(9, 2);
    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);
    marker.value = shopID;

    return marker;
}



function startPopupShop() {
    stopPopupFlag = false;
    popupNum = 0;
    loopPopupShop();
}

function stopPopupShop() {
    stopPopupFlag = true;
}

function resumePopupShop() {
    stopPopupFlag = false;
    loopPopupShop();
}

function loopPopupShop() {
    window.setTimeout(function() {
        if (stopPopupFlag == true) {
            stopPopupFlag = false;
            return;
        }
        var totalNum = shopList.length;
        var num = totalNum - 1 - popupNum;
        var currentMarker = shopList[num];
        GEvent.trigger(currentMarker, "popup");

        popupNum += 1;

        if (popupNum >= shopList.length)
            popupNum = 0;

        loopPopupShop();
    }

    , 3000);
}